<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Dialog</title>
  <style>
    .dialog-container {
      max-width: 800px;
      margin: 30px auto;
      border-radius: 4px;
      box-shadow: 0 0 4px 0px rgba(0, 0, 0, 0.3);
      padding: 16px;
    }

    .button {
      padding: 8px 16px;
      font-size: 14px;
      font-weight: 500;
      color: #303030;
      border: 1px solid #ccc;
      border-radius: 4px;
      outline: none;
      cursor: pointer;
      position: relative;
      background-color: inherit;
    }

    .button:hover {
      border-color: lightskyblue;
      color: lightskyblue;
    }

    .button.primary {
      background-color: lightskyblue;
      color: #fff;
      border: 0;
    }

    .dialog {
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background-color: rgba(0, 0, 0, .3);
      display: none;
      opacity: 0;
      transition: all .6s;
    }

    .dialog.show {
      display: block;
    }

    .dialog.appear {
      opacity: 1;
    }

    .dialog .main {
      width: 40%;
      background-color: #fff;
      margin: 30px auto;
      padding: 16px;
      border-radius: 6px;
      box-shadow: 0 0 4px 0 rgba(0, 0, 0, 0.3);
      transform: translateY(-100%);
      opacity: 0;
      transition: all .6s;
    }

    .dialog.appear .main {
      opacity: 1;
      transform: translateY(100%);
    }

    .dialog .header {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 18px;
    }

    .dialog .header .close {
      margin-left: auto;
      display: flex;
      align-items: center;
      justify-content: center;
      border: none;
      outline: none;
      padding: 8px;
      cursor: pointer;
      background-color: inherit;
    }

    .dialog .header .close::before,
    .dialog .header .close::after {
      content: '';
      position: absolute;
      display: block;
      width: 10px;
      height: 1px;
      background: #333;
      transform: rotate(45deg);
    }

    .dialog .header .close::after {
      transform: rotate(-45deg);
    }

    .dialog .footer {
      display: flex;
      justify-content: flex-end;
    }

    .dialog .footer .button {
      margin-left: 10px;
    }
  </style>
</head>

<body>
  <div class="dialog-container">
    <h2>Dialog</h2>
    <button class=" button open-dialog">打开弹窗</button>
    <div class="dialog">
      <div class="main">
        <div class="header">
          提示
          <button class="close"></button>
        </div>
        <div class="content">
          <p>
            这是一条信息
          </p>
        </div>
        <div class="footer">
          <button class="button btn-cancel">取消</button>
          <button class="button primary btn-ok">确定</button>
        </div>
      </div>
    </div>
  </div>

  <script>
    class Dialog {
      constructor($root, options = {}) {
        this.$root = $root
        this.options = options
        this.onCancel = options.onCancel || function () { }
        this.onOk = options.onOk || function () { }

        this.bind()
      }
      bind() {
        let self = this
        this.$root.querySelector('.close').onclick = function () {
          self.hide()
          self.onCancel()
        }
        this.$root.querySelector('.btn-cancel').onclick = function () {
          self.hide()
          self.onCancel()
        }
        this.$root.querySelector('.btn-ok').onclick = function () {
          self.hide()
          self.onOk()
        }
      }
      hide() {
        this.$root.classList.remove('appear')
        setTimeout(() => this.$root.classList.remove('show'), 400)
      }
      show() {
        this.$root.classList.add('show')
        setTimeout(() => this.$root.classList.add('appear'))
      }
    }

    let dialog = new Dialog(document.querySelector('.dialog'), {
      onOk() {
        console.log('ok');

      },
      onCancel() {
        console.log('cancel');

      }
    })
    document.querySelector(".open-dialog").onclick = function () {
      dialog.show()
    }

  </script>
</body>

</html>